package application;
	
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.StringUtils;
import org.james.deleter.components.AlertWindow;
import org.james.deleter.components.FileChooseGrid;
import org.james.deleter.components.InfoListBox;
import org.james.deleter.task.FileHandleTask;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class Main extends Application {
	
	public static final Button execBtn = new Button("确定");

	
	@Override
	public void start(final Stage primaryStage) {
		try {
            ExecutorService executorService = Executors.newCachedThreadPool();
            
			primaryStage.setFullScreen(false);
			primaryStage.setResizable(false);
			BorderPane root = new BorderPane();
			
			FileChooseGrid fileChooseGrid = new FileChooseGrid();
			InfoListBox infoListBox = new InfoListBox();
			
			HBox execBox = new HBox();
			execBox.getChildren().addAll(execBtn);
			execBox.setAlignment(Pos.CENTER);
			execBox.setPadding(new Insets(15, 12, 10, 12));
			execBox.setSpacing(45); 
			execBtn.setOnAction((event) -> {
				if(StringUtils.isBlank(fileChooseGrid.getTextField1().getText())){
					AlertWindow.show("请选择目录");
					return;
				}
				String operateType = fileChooseGrid.getOperateTypeGroup().getSelectedToggle().getUserData().toString();
				String deteleType = fileChooseGrid.getDeteleTypeGroup().getSelectedToggle().getUserData().toString();
				String folder = fileChooseGrid.getTextField1().getText();
				if(StringUtils.equals("operateType1", operateType)){
					if(StringUtils.equals("deteleType1", deteleType)){
						if(StringUtils.isBlank(fileChooseGrid.getFileNameField().getText())){
							AlertWindow.show("请选择后缀名！");
							return;
						}
						InfoListBox.getMessageList().setItems(FXCollections.observableArrayList());
						execBtn.setText("正在执行...");
						execBtn.setDisable(true);
						InfoListBox.addMessageInfo("操作类型：根据后缀名删除文件......");
						InfoListBox.addMessageInfo("操作目录："+folder+"......");
						String extensions = fileChooseGrid.getFileNameField().getText();
						FileHandleTask filetask = new FileHandleTask("1",folder,extensions,fileChooseGrid.getIsRecurDelete().isSelected());
						executorService.submit(filetask);		
					} else if(StringUtils.equals("deteleType2", deteleType)){
						if(StringUtils.isBlank(fileChooseGrid.getFileNameField().getText())){
							AlertWindow.show("请输入文件名！");
							return;
						}
						InfoListBox.getMessageList().setItems(FXCollections.observableArrayList());
						execBtn.setText("正在执行...");
						execBtn.setDisable(true);
						InfoListBox.addMessageInfo("操作类型：根据匹配关键字删除文件......");
						InfoListBox.addMessageInfo("操作目录："+folder+"......");
						String keywords = fileChooseGrid.getFileNameField().getText();
						FileHandleTask filetask = new FileHandleTask("2",folder,keywords,fileChooseGrid.getIsRecurDelete().isSelected());
						executorService.submit(filetask);
					}else{
						AlertWindow.show("功能尚未开发！");
						return;
					}
				}else if(StringUtils.equals("operateType3", operateType)){
					InfoListBox.getMessageList().setItems(FXCollections.observableArrayList());
					execBtn.setText("正在执行...");
					execBtn.setDisable(true);
					InfoListBox.addMessageInfo("操作类型：删除当前目录......");
					InfoListBox.addMessageInfo("操作目录："+folder+"......");
					FileHandleTask filetask = new FileHandleTask("3",folder);
					executorService.submit(filetask);	
				}else{
					if(StringUtils.isBlank(fileChooseGrid.getFileNameField().getText())){
						AlertWindow.show("请输入文件名！");
						return;
					}
					execBtn.setText("正在执行...");
					execBtn.setDisable(true);
					InfoListBox.addMessageInfo("操作类型：删除文件和文件夹......");
					InfoListBox.addMessageInfo("操作目录："+folder+"......");
					String keyword = fileChooseGrid.getFileNameField().getText();
					FileHandleTask filetask = new FileHandleTask("2",folder,keyword,true);
					executorService.submit(filetask);	
//					return;
				}
			});
			
			root.setTop(fileChooseGrid);
			root.setCenter(execBox);
			root.setBottom(infoListBox);
			Scene scene = new Scene(root,800,500);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
			primaryStage.setOnCloseRequest((e) ->{
				System.exit(0);
			});
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		launch(args);
	}
}
